const csv=`
name,age,parent
Bob,30,David
David,60,
Anna,10,Bob
`;
const processor=(data,delimiters=',')=>{
    data=data.trim();
    // console.log(data.slice(0,data.indexOf('\n)));
    const titlesData=data.slice(0,data.indexOf('\n'));// begin end split(delimiter) arr
    // console.log(titlesData);
    const titles=titlesData.split(delimiters);
    // console.log(titles)
    const rows=data
        .slice(data.indexOf('\n')+1)
        .split('\n')
    // console.log(rows);
    const rowData=rows.map(v=>{
        const values=v.split(delimiters);
        // reducer 第二个参数 起始值 obj是上一次的返回的值
        return titles.reduce((obj,title,index)=>{
            if(values[index]){
                obj[title]=values[index]
            }else{
                obj[title]=''
            }
            return obj // 这一次的返回
        },{})
    })
    // console.log(rowData)

    return rowData.map(row=>{
        let person={
            ...row
        }
        let parents=rowData.filter(v=> v.name==row.parent);
        let children=rowData.filter(v=>v.parent==row.name);
        if(parents.length){
            person['parent']=parents.map(parent=>({
                name:parent.name,
                age:parent.age
            }))
        }else{
            person['parent']=[]
        }
        if(children.length){
            person['children']=children.map(child=>({
                name:child.name,
                age:child.age 
            }))
        }else{
            person['children']=[]
        }
        // children
        // parent
        return person 
    })
}
console.log(processor(csv))